import { login, register, getUserInfo, logout, forgetPassword, updatePassword, bindPhone, moneyCode, addBankCard, getBankCard, addIdTent, getIdTentInfo, isCompleteCertification} from "@/api/user"
import { Message } from "element-ui";
import router from "@/router";

const userOptions = {
    namespaced: true,
    actions: {
        // 注册
        async Register(context, data) {
            let result = await register(data)
            if (result.code === 200) {
                Message({ type: 'success', message: result.message })
            } else {
                Message({ type: 'error', message: result.message })
            }
            
        },
        // 登录
        async Login(context, data) {
            let result = await login(data)
            if (result.code === 200) {
                Message({ type: 'success', message: result.message })
                context.commit('LOGIN', result)
                context.dispatch('getUser', result.token)
            } else {
                Message({ type: 'error', message: result.message })
            }
        },
        // 获取用户信息
        async getUser(context, token) {
            let userInfo = await getUserInfo({token})
            if (userInfo.code === 200) {
                context.commit('GETUSERINFO', userInfo.data)
            }
        },
        // 退出登录
        async Logout(context, id) {
            let result = await logout(id)
            if (result.code === 200) {
                context.commit('LOGOUT')
            }
        },
        // 忘记密码
        async ForgetPassword(context, data) {
            let result = await forgetPassword(data)
            if (result.code === 200) {
                Message({ type: 'success', message: result.message })
                router.push({name: 'Login'})
            } else {
                Message({ type: 'error', message: result.message })
            }
            
        },
        // 修改密码
        async UpdatePassword(context, data) {
            let result = await updatePassword(data)
            if (result.code === 200) {
                Message({ type: 'success', message: result.message })
                context.commit('LOGOUT')
            } else {
                Message({ type: 'error', message: result.message })
            }
        },
        // 绑定手机号
        async BindPhone(context, data) {
            let result = await bindPhone(data)
            if (result.code === 200) {
                Message({ type: 'success', message: result.message })
            } else {
                Message({ type: 'error', message: result.message })
            }
        },
        // 设置资金密码
        async SetMoneyCode(context, data) {
            let result = await moneyCode(data)
            if (result.code === 200) {
                Message({ type: 'success', message: result.message })
            } else {
                Message({ type: 'error', message: result.message })
            }
        },
        // 添加银行卡
        async AddBankCard(context, data) {
            let result = await addBankCard(data)
            if (result.code === 200) {
                Message({ type: 'success', message: result.message })
                router.replace({name: 'BindBankCard'})
            } else {
                Message({ type: 'error', message: result.message })
            }
        },
        // 获取银行卡信息
        async GetBankCard(context, id) {
            let result = await getBankCard(id)
            if (result.code === 200) {
                context.commit('GETBANKCARD', result.data)
            }
        },
        // 身份认证
        async AddIdTent(context, data) {
            let result = await addIdTent(data)
            if (result.code === 200) {
                Message({ type: 'success', message: result.message })
                router.replace({name: 'Authentication'})
            } else {
                Message({ type: 'error', message: result.message })
            }
        },
        // 获取身份认证信息
        async GetIdTentInfo(context, id) {
            let result = await getIdTentInfo(id)
            if (result.code === 200) {
                context.commit('GETIDTENTINFO', result.data)
            }
        },
        // 查看是否完成认证
        async IsCompleteCertification(context, id) {
            let result = await isCompleteCertification(id)
            if (result.code === 200) {
                let { bindBank, drivingLicence, isIdentification } = result.data
                if (!isIdentification && !drivingLicence && !bindBank) {
                    Message({ type: 'warning', message: '请实名认证并绑定驾驶证和银行卡' })
                } else if (!isIdentification) {
                    Message({ type: 'warning', message: '请先实名认证' })
                } else if (!drivingLicence) {
                    Message({ type: 'warning', message: '请先绑定您的驾驶证' })
                } else if (!bindBank) {
                    Message({ type: 'warning', message: '请绑定您的银行卡' })
                } else {
                    context.commit('authentication')
                }
            } else {
                Message({ type: 'error', message: result.message })
            }
        }
    },
    mutations: {
        LOGIN(state, data) {
            state.token = data.token
            state.user_id = data.id
            localStorage.setItem('token', data.token)
        },
        GETUSERINFO(state, data) {
            state.userInfo = data
            localStorage.setItem('userInfo', JSON.stringify(data))
            router.push({name: 'User'})
        },
        LOGOUT(state, data) {
            state.user_id = ''
            state.token = ''
            state.userInfo = {}
            localStorage.removeItem('token')
            localStorage.removeItem('userInfo')
            router.push({name: 'Login'})
        },
        GETBANKCARD(state, data) {
            state.cardInfo = data
        },
        GETIDTENTINFO(state, data) {
            state.idTentInfo = data[0] || {}
        },
        authentication(state, data) {
            state.authentication = true
        }
    },
    state: {
        // user_id: JSON.parse(localStorage.getItem('userInfo')).id || '',
        user_id: '',
        token: localStorage.getItem('token') || '',
        userInfo: JSON.parse(localStorage.getItem('userInfo')) || {},
        cardInfo: [],
        idTentInfo: {},
        authentication: true
    },
    getters: {}
}
export default userOptions